//
//  MessageViewController.m
//  zhijian
//
//  Created by lcb on 2019/1/19.
//  Copyright © 2019 lcb. All rights reserved.
//

#import "MessageViewController.h"
#import "MessageModel.h"
@interface MessageViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)UITableView *tableView;
@property (nonatomic, strong) BaseRefresh *ymRefresh;
@property(nonatomic,strong) NSMutableArray *dataSource;
@property(nonatomic,assign) NSInteger currentPage;
@property(nonatomic,assign) BOOL isLoading;
@end

@implementation MessageViewController
@synthesize dataSource=_dataSource;
@synthesize currentPage=_currentPage;
@synthesize isLoading=_isLoading;
- (void)viewDidLoad {
    [super viewDidLoad];
    [self hyb_navWithLeftImage:@"back" title:@"我的消息" leftClicked:^(UIButton *sender) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
    if (!_dataSource) {
        _dataSource =[NSMutableArray array];
    }
    self.currentPage =1;
    self.isLoading = NO;
    //eg:3
    _ymRefresh = [[BaseRefresh alloc] init];
    __weak __typeof(self)weakSelf = self;
    [_ymRefresh normalModelRefresh:self.tableView refreshType:RefreshTypeDouble firstRefresh:YES timeLabHidden:NO stateLabHidden:YES dropDownBlock:^{
        NSLog(@"下拉");
        if ([weakSelf.tableView.mj_footer isRefreshing]) {
            return;
        }
        weakSelf.currentPage = 1;
        [weakSelf loadData];
    } upDropBlock:^{
        NSLog(@"上拉");
        if ([weakSelf.tableView.mj_header isRefreshing]) {
            return;
        }
        weakSelf.currentPage++;
        [weakSelf loadData];
    }];
    
}

-(UITableView *)tableView{
    
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:(UITableViewStyleGrouped)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView];
    }
    return _tableView;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}
//section底部间距
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}
//section底部视图
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MessageModel"];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"MessageModel"];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    MessageModel *qModel =[self.dataSource objectAtIndex:indexPath.row];
    cell.textLabel.text =qModel.content;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    MessageModel *qModel =[self.dataSource objectAtIndex:indexPath.row];
    if (qModel.isRead==0) {
        [[Api sharedApi] readReplyInfo:[NSString stringWithFormat:@"%ld",qModel.messageId] callback:nil];
    }
}

-(void)loadData
{
    self.isLoading =YES;
    __weak __typeof(self)weakSelf = self;
    [[Api sharedApi] getMyReplyInfo:[NSString stringWithFormat:@"%ld",(long)self.currentPage] pageSize:@"20" Callback:^(ApiResponse *response,NSString* errorMsg) {
        self.isLoading =NO;
        if (response!=nil && response.code==0 && response.data ) {
            NSArray * listModel = [NSArray yy_modelArrayWithClass:[MessageModel class] json:response.data];
            if (self.currentPage==1) {
                [self.dataSource removeAllObjects];
                self.dataSource = [NSMutableArray arrayWithArray:listModel];
            }else{
                [self.dataSource addObjectsFromArray:listModel];
            }
            if ([weakSelf.tableView.mj_header isRefreshing]) {
                [weakSelf.tableView.mj_header endRefreshing];
                if (listModel.count==0) {
                    [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
                }
            }
            if ([weakSelf.tableView.mj_footer isRefreshing]) {
                if (listModel.count==0) {
                    [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
                }else{
                    [weakSelf.tableView.mj_footer endRefreshing];
                }
            }
        }else{
            [BaseToast show:response.message];
        }
        
        [self.tableView reloadData];
    }];
}
@end
